package com.travel.server.manager.impl.command.project;

import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.PageDto;
import com.travel.server.manager.impl.dal.GroupDal;
import com.travel.server.manager.dto.request.PageRequest;
import com.travel.server.manager.dto.response.project.QueryProjectGroupResponse;
import com.travel.server.manager.dto.response.project.QueryProjectGroupResponse.GroupItem;

import com.travel.server.manager.model.ProjectGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/10/26 16:56
 * @Desc:
 */
@Component
@Slf4j
public class QueryProjectGroupCommand extends AbstractCommand<PageRequest, QueryProjectGroupResponse> {

    @Autowired
    private GroupDal groupDal;

    @Override
    protected void checkInput(PageRequest request) {
        checkPageInput(request);
    }

    @Override
    public QueryProjectGroupResponse handle(PageRequest request) {
        this.checkPageInput(request);

        PageDto<ProjectGroup> pageDto = groupDal.select(request.getOffset(), request.getLimit());

        List<ProjectGroup> list = pageDto.getList();

        List<GroupItem> groupList = new ArrayList<>();
        if(!CollectionUtil.isEmpty(list)){
            for(ProjectGroup projectGroup : list){
                GroupItem groupItem = new GroupItem();
                groupItem.setNumber(projectGroup.getNumber());
                groupItem.setGroupName(projectGroup.getName());
                groupItem.setParentClass(projectGroup.getParentClass());
                groupItem.setCreateTime(projectGroup.getCreateTime());
                groupItem.setId(projectGroup.getId());
                groupList.add(groupItem);
            }
        }

        QueryProjectGroupResponse response = new QueryProjectGroupResponse();
        response.setTotal(pageDto.getTotal());
        response.setGroupList(groupList);
        return response;
    }
}
